﻿#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;

namespace bit {
	class string {
	public:
		typedef char* iterator;
		iterator begin() { return _str; }
		iterator end() { return _str + _size; }
		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size) {
			//cout << "string(char* str)" << endl;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		// s1.swap(s2)
		void swap(string& s) {
			::swap(_str, s._str);
			::swap(_size, s._size);
			::swap(_capacity, s._capacity);
		}
		// 拷贝构造
		string(const string& s) { //const左值引用既可以引用左值，也可以引用右值
			cout << "string(const string& s) -- 拷贝构造 深拷贝" << endl;
			string tmp(s._str);
			swap(tmp);
		}

		//移动构造
		string(string&& s) {
			cout << "string(string&& s) -- 移动构造" << endl;
			swap(s);//this：指向正在构造的临时对象（内存由编译器分配）
			//临时对象不新开空间
		}

		// 赋值重载
		string& operator=(const string& s) {
			cout << "string& operator=(string s) -- 赋值重载 深拷贝" << endl;
			//string tmp(s);
			//swap(tmp);
			if (this != &s) {
				char* tmp = new char[s._capacity + 1];
				strcpy(tmp, s._str);
				delete[] _str;
				_str = tmp;
				_size = s._size;
				_capacity = s._capacity;
			}
			return *this;
		}
		string& operator=(string&& s) {
			cout << "string& operator=(string&& s) -- 移动赋值" << endl;
			swap(s);
			return *this;
		}

		~string() {
			delete[] _str;
			_str = nullptr;
		}
		char& operator[](size_t pos) {
			assert(pos < _size);
			return _str[pos];
		}
		void reserve(size_t n) {
			if (n > _capacity) {
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch) {
			if (_size >= _capacity) {
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}
		//string operator+=(char ch)
		string& operator+=(char ch) {
			push_back(ch);
			return *this;
		}
		const char* c_str() const { return _str; }
	private:
		char* _str = nullptr;
		size_t _size = 0;
		size_t _capacity = 0; // 不包含最后做标识的\0
	};

	bit::string to_string(int x) {
		bit::string ret;
		while (x) {
			int val = x % 10;
			x /= 10;
			ret += ('0' + val);
		}
		reverse(ret.begin(), ret.end());
		return ret;
	}
}


class Person {
public:
	Person(const char* name = "", int age = 0)
		:_name(name)
		, _age(age) {
	}
	//Person(const Person& p)
	//	:_name(p._name)
	//	, _age(p._age) {
	//}

	//Person& operator=(const Person& p) {
	//	if (this != &p) {
	//		_name = p._name;
	//		_age = p._age;
	//	}
	//	return *this;
	//}

	//强制生成默认函数的关键字default
	Person(const Person& p) = default;
	Person(Person&& p) = default;

	//禁止生成默认函数的关键字delete
	//Person(const Person& p) = delete;

	//~Person() {}
private:
	bit::string _name;
	int _age;
};
int main() {
	//屏蔽Person(const Person& p)、Person& operator=(const Person& p)、~Person()
	Person s1;
	Person s2 = s1;//默认的拷贝构造 string(const string& s)
	Person s3 = std::move(s1);//默认的移动构造 string(string&& s)
	//Person s4;
	//s4 = std::move(s2);
	return 0;
}